from django.db import models

# Create your models here.
# class BaseMode(models.Model):
#     create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
#     update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
#
#     class Meta:
#         abstract = True  # 它是一个抽象类，默认允许下面类继承


# 用户模块（用户表，区域表，用户地址表）
# 用户表
# class Users(BaseMode):
#     username = models.CharField(max_length=50)
#     password = models.CharField(max_length=400)
#     phone = models.CharField(max_length=11)
#     class Meta:
#         db_table = 'users'
# # 区域表
# class Area(BaseMode):
#     name = models.CharField(max_length=100)
#     pid = models.ForeignKey('self',on_delete=models.SET_NULL,null=True,blank=True)
#     class Meta:
#         db_table='area'
#
#
# class Uaddress(BaseMode):
#     users = models.ForeignKey(Users,models.CASCADE)
#     area = models.ForeignKey(Area,models.CASCADE)

class Journalism(models.Model): # 新闻
    headline = models.CharField(max_length=50)
    contemt = models.TextField(default='')
    pic = models.ImageField()
    class Meta:
        db_table='journalism'



class Advertising(models.Model):
    pic = models.ImageField(default='',verbose_name='图片')
    urls = models.CharField(max_length=50,verbose_name='标题')
    type = models.IntegerField(verbose_name='类型')  # 1.轮播图  2.右边小图

    class Meta:
        db_table = 'advertising'


class Area(models.Model):
    name = models.CharField(max_length=100)
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True)

    class Meta:
        db_table = 'tb_areas'

    def __str__(self):
        return self.name


class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class User(BaseModel):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=300)
    mobile = models.CharField(max_length=11)

    class Meta:
        db_table = 'user_info'


# 地址表
class Address(BaseModel):
    # 用户id(外键关联到userid),receive（接收人），省，市，区，详细地址，手机号，电话，邮箱，是否删除，默认地址（int）
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    receive = models.CharField(max_length=50, verbose_name='接收人')
    province = models.ForeignKey('Area', on_delete=models.CASCADE, related_name='province')
    city = models.ForeignKey('Area', on_delete=models.CASCADE, related_name='city')
    area = models.ForeignKey('Area', on_delete=models.CASCADE, related_name='area')
    address = models.CharField(max_length=255)
    mobile = models.CharField(max_length=11)
    tel = models.CharField(max_length=20)
    email = models.EmailField()
    is_del = models.IntegerField(default=0)  # 1删除  0未删除
    is_default = models.IntegerField(default=0)  # 1默认


# 分类
class Category(BaseModel):
    """
    商品类别
    """
    name = models.CharField(max_length=10, verbose_name='名称')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='父类别',
                               related_name='subs')
    pic = models.ImageField(null=True, blank=True)
    level = models.IntegerField(default=1)
    top_parent = models.IntegerField(default=0)

    class Meta:
        db_table = 'category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(BaseModel):
    """
    商品 简介，价格，添加时间，图片，内容，售后服务，规格说明，库存，是否首页
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    category1 = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='cat1_goods', verbose_name='一级类别')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='cat_goods', verbose_name='类别')
    sales = models.IntegerField(default=0, verbose_name='销量')
    comments = models.IntegerField(default=0, verbose_name='评价数')
    content = models.TextField()
    service = models.CharField(max_length=100)
    specs = models.CharField(max_length=100, verbose_name='规格说明')
    store = models.IntegerField(default=9999, verbose_name='库存')
    home_show = models.IntegerField(default=0, verbose_name='首页展示')  # 0不展示 1展示
    pic = models.ImageField(null=True, blank=True)
    price = models.DecimalField(max_digits=7, decimal_places=2)

    class Meta:
        db_table = 'goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 评价表
class Comment(BaseModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    content = models.TextField()

    class Meta:
        db_table = 'comment'


class OrderInfo(BaseModel):
    """
    订单信息
    """

    PAY_METHOD_CHOICES = (
        (1, "货到付款"),
        (2, "支付宝"),
    )

    ORDER_STATUS_CHOICES = (
        (1, "待支付"),
        (2, "待发货"),
        (3, "待收货"),
        (4, "待评价"),
        (5, "已完成"),
        (6, "已取消"),

    )

    order_no = models.CharField(max_length=64, primary_key=True, verbose_name="订单号")
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="下单用户")
    address = models.CharField(max_length=255, verbose_name="收货地址")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总金额")
    pay_method = models.IntegerField(choices=PAY_METHOD_CHOICES, default=1, verbose_name="支付方式")
    status = models.IntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name="订单状态")
    transaction_no = models.CharField(max_length=100)

    class Meta:
        db_table = "order_info"
        verbose_name = '订单基本信息'
        verbose_name_plural = verbose_name


class OrderDetail(BaseModel):
    """
    订单商品
    """
    SCORE_CHOICES = (
        (0, '0分'),
        (1, '20分'),
        (2, '40分'),
        (3, '60分'),
        (4, '80分'),
        (5, '100分'),
    )
    order = models.ForeignKey(OrderInfo, to_field='order_no', on_delete=models.SET_NULL, blank=True, null=True,
                              verbose_name="订单")
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    goodsname = models.CharField(max_length=100)
    images = models.CharField(max_length=100)
    count = models.IntegerField(default=1, verbose_name="数量")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="单价")
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = "order_detail"
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name




class Classify(models.Model):
    name = models.CharField(max_length=50)
    class Meta:
        db_table='classify'


class Books(models.Model):
    name = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='价格')
    Classify = models.ForeignKey(Classify,on_delete=models.CASCADE,related_name='book')
    class Meta:
        db_table= 'books'
